package com.gene.Filter;

import com.gene.pojo.UpmsUser;
import com.gene.service.IUpmsUserService;
import com.gene.service.impl.UpmsUserServiceImpl;
import com.gene.utils.*;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/1/15.
 * 用户登录过滤器
 */
public class LoginFilter implements Filter {
    /**
     * 判断session中是否有用户的信息:
     * * session中如果有:放行.
     * * session中没有:
     *    * 从Cookie中获取:
     *        * Cookie中没有:放行.
     *        * Cookie中有:
     *            * 获取Cookie中存的用户名和密码到数据库查询.
     *                * 没有查询到:放行.
     *                * 查询到:将用户信息存入到session . 放行.
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain Chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse)response;
        HttpSession session = req.getSession();
        System.out.println(req.getServletPath());
        List<String> urllist = new ArrayList<String>();
        urllist.add("/upmsUser/login");
        urllist.add("/upmsUser/register");
        urllist.add("/upmsUser/sms");
        urllist.add("/upmsUser/idcar");
        urllist.add("/upmsUser/userName");
        urllist.add("//index.jsp");
        urllist.add("/index.html");
        urllist.add("/index");
        UpmsUser existUser =(UpmsUser) session.getAttribute(Const.SESSION_USER);
        if (true/*existUser!= null || req.getServletPath().endsWith(".css") || req.getServletPath().endsWith(".js")||urllist.contains(req.getServletPath())|| req.getServletPath ().contains(".do")*/) {
            //已登录
            Chain.doFilter(req,response);
        }
        else {
            // session中没有用户信息.
            // 获得Cookie的数据:
            Cookie[] cookies = req.getCookies();
            Cookie cookie = CookieUtils.findCookie(cookies, "autoLogin");
            if(cookie==null){
                //没有携带cookie
                res.sendRedirect("/gene-safe/index");
            }else {
                // 带着Cookie信息过来.
                String value = cookie.getValue();// aaa#111
                // 获得用户名和密码:
                String username = value.split("#")[0];
                String password = value.split("#")[1];
                IUpmsUserService upmsUserService=new UpmsUserServiceImpl();
                UpmsUser user = upmsUserService.selectUserNameAndPassword(username, password);
                if (null == user) {
                    // 用户名或密码错误:Cookie被篡改的.
                    res.sendRedirect("/gene-safe/index.jsp");
                }else {
                    boolean verify = MD5Util.verify(password, user.getPassword());
                    if (!verify) {
                        //不一致
                        res.sendRedirect("/gene-safe/index.jsp");
                    }else {
                        // 将用户存到session中,放行
                        session.setAttribute(Const.SESSION_USER, user);
                        Chain.doFilter(req, response);
                    }
                }


            }
        }
    }



    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }



    @Override
    public void destroy() {

    }
}
